package com.ross.web.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.OrderBy;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import sun.awt.geom.AreaOp.AddOp;

import com.ross.web.domain.Product;
import com.ross.web.domain.SoldProducts;
import com.ross.web.util.HibernateUtil;

public class SoldProductsDAO extends HibernateUtil {
	
	public List getAllSoldProducts(int limit){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			result = session.createCriteria(SoldProducts.class, "sp")
					.setProjection(Projections.projectionList()
							.add(Projections.property("sp.sp_id"),"sp_id")
							.add(Projections.property("sp.prodId"),"prodId")
							.add(Projections.property("sp.units"),"units")
							.add(Projections.property("sp.solddate"),"solddate")
							.add(Projections.property("sp.product"),"product")
							.add(Projections.property("sp.prodname"),"prodname")
							)
							.addOrder(Order.desc(("sp.units")))
							.setMaxResults(limit)
							.setResultTransformer(new AliasToBeanResultTransformer(SoldProducts.class))
							.list();
				
				
			return result;
			
		}finally{
			session.close();
		}
		
	}
	
	public SoldProducts getSoldProductsByProdId(int prodId){
		
		Session session = HibernateUtil.getSession();
		List result = new LinkedList();
		
		try{
			
			result = session.createCriteria(SoldProducts.class, "sp")
					.setProjection(Projections.projectionList()
							.add(Projections.property("sp.sp_id"),"sp_id")
							.add(Projections.property("sp.prodId"),"prodId")
							.add(Projections.property("sp.units"),"units")
							.add(Projections.property("sp.solddate"),"solddate")
							.add(Projections.property("sp.product"),"product")
							.add(Projections.property("sp.prodname"),"prodname")
							)
							
							.add(Restrictions.eq("sp.prodId", prodId))
							
							.setResultTransformer(new AliasToBeanResultTransformer(SoldProducts.class))
							.list();
				
				
			return result.isEmpty() ? null : (SoldProducts) result.get(0);
			
		}finally{
			session.close();
		}
		
	}
	
	public void insert(SoldProducts sp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.save(sp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void update(SoldProducts sp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.update(sp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}
	
	public void delete(SoldProducts sp){
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		
		try{
			tx = session.beginTransaction();
			session.delete(sp);
			tx.commit();
		}catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
		
	}

}
